package com.aries.ui.view.radius;


import com.aries.ui.view.radius.delegate.RadiusEditTextDelegate;
import ohos.agp.components.AttrSet;
import ohos.agp.components.Component;
import ohos.agp.components.TextField;
import ohos.agp.utils.TextTool;
import ohos.app.Context;

/**
 * @Author: AriesHoo on 2018/7/19 10:03
 * @E-Mail: AriesHoo@126.com
 * Function: 用于需要圆角矩形框背景的EditText的情况,减少直接使用EditText时引入的shape资源文件
 * Description:
 * 1、2018-2-5 14:27:16 初始化TextView的 RadiusTextDelegate
 * 2、2018-6-13 11:28:09 默认设置不可点击
 */
public class RadiusEditText extends TextField implements Component.EstimateSizeListener {

    /**
     * 是否设置完成光标标识
     */
    private boolean mSelectionEndDone;
    private RadiusEditTextDelegate delegate;

    public RadiusEditText(Context context) {
        this(context, null);
    }

    public RadiusEditText(Context context, AttrSet attrs) {
        this(context, attrs, "");
    }

    public RadiusEditText(Context context, AttrSet attrs, String defStyleAttr) {
        super(context, attrs, defStyleAttr);
        delegate = new RadiusEditTextDelegate(this, context, attrs);
       // setFocusableInTouchMode(true);
        setTouchFocusable(true);
        setEstimateSizeListener(this::onEstimateSize);
        init();
    }
    private void init(){
        if (delegate == null) {
            return ;
        }
        getContext().getUITaskDispatcher().delayDispatch(new Runnable() {
            @Override
            public void run() {
                if (delegate.getRadiusHalfHeightEnable()) {
                    delegate.setRadius((float) (getEstimatedHeight() / (double)2));
                }
                delegate.init();
            }
        }, 10);
    }
    /**
     * 获取代理类用于Java代码控制shape属性
     *
     * @return RadiusEditTextDelegate
     */
    public RadiusEditTextDelegate getDelegate() {
        return delegate;
    }


    @Override
    public void setText(String text) {
        super.setText(text);
        if (TextTool.isNullOrEmpty(text)) {
            return;
        }
        if (!delegate.isSelectionEndEnable()) {
            return;
        }
        if (mSelectionEndDone) {
            return;
        }
       // setSelection(text.length());todo  无法更改光标位置
        mSelectionEndDone = delegate.isSelectionEndOnceEnable();
    }




    @Override
    public boolean onEstimateSize(int i, int i1) {
        if (delegate == null) {
            return true;
        }
        return false;
    }



    @Override
    public void setSelected(boolean selected) {
        super.setSelected(selected);
        if (delegate != null) {
            delegate.setSelected(selected);
        }
    }

    @Override
    public void setEnabled(boolean enabled) {
        super.setEnabled(enabled);
        if (delegate != null) {
            delegate.init();
        }
    }

    @Override
    public void setPressState(boolean pressState) {
        super.setPressState(pressState);
        if (delegate != null) {
            delegate.init();
        }
    }




}
